package com.yanghui.service.impl;

import com.yanghui.dao.BlackIpMapper;
import com.yanghui.pojo.BlackIp;
import com.yanghui.pojo.BlackIpExample;
import com.yanghui.service.BlackIpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BlackIpServiceImpl implements BlackIpService {
    @Autowired
    BlackIpMapper blackIpMapper;


    /**
     * 将某个IP加入到黑名单
     * 注意需要考虑IP是否重复
     */
    @Override
    public int addBlackIp(BlackIp blackIp) {
        BlackIpExample blackIpExample = new BlackIpExample();
        BlackIpExample.Criteria criteria = blackIpExample.createCriteria();

        criteria.andIpEqualTo(blackIp.getIp());
        List<BlackIp> blackIpList = blackIpMapper.selectByExample(blackIpExample);
        if (blackIpList.size() > 0) {
            return -1;
        }

        return blackIpMapper.insertSelective(blackIp);
    }

    /**
     * 按照 IP 查询对应的记录
     */
    @Override
    public List<BlackIp> selectByIp(String ip) {
        BlackIpExample blackIpExample = new BlackIpExample();
        BlackIpExample.Criteria criteria = blackIpExample.createCriteria();
        criteria.andIpEqualTo(ip);
        return blackIpMapper.selectByExample(blackIpExample);
    }

    /**
     * 按照访客ID删除黑名单
     */
    @Override
    public int deleteBlackIpByID(Integer id) {
        BlackIpExample blackIpExample = new BlackIpExample();
        BlackIpExample.Criteria criteria = blackIpExample.createCriteria();

        criteria.andVidEqualTo(id);

        return blackIpMapper.deleteByExample(blackIpExample);
    }

    /**
     * 按照指定条件查询黑名单
     */
    @Override
    public List<BlackIp> selectLikeBlackIpListByPage(Map<String, Object> map) {
        BlackIpExample blackIpExample = new BlackIpExample();
        BlackIpExample.Criteria criteria = blackIpExample.createCriteria();

        /** 添加条件到 SQL 语句中　*/
        try {
            if (map.get("ip") != null) {
                criteria.andIpLike("%" + map.get("ip") + "%");
            }
            if (map.get("city") != null) {
                criteria.andCityLike("%" + map.get("city") + "%");
            }
            if (map.get("platformType") != null) {
                criteria.andPlatformTypeLike("%" + map.get("platformType") + "%");
            }
            if (map.get("browserType") != null) {
                criteria.andBrowserTypeLike("%" + map.get("browserType") + "%");
            }
            if (map.get("startTime") != null && map.get("endTime") != null) {
                Date startTime = new SimpleDateFormat("yyyy-MM-dd").parse((String) map.get("startTime"));
                Date endTime = new SimpleDateFormat("yyyy-MM-dd").parse((String) map.get("endTime"));
                criteria.andTimeBetween(startTime, endTime);
            }
        } catch (Exception e) {
            System.out.println("selectLikeBlackIpListByPage");
        }

        return blackIpMapper.selectLikeBlackIpListByPage(blackIpExample);
    }

    /**
     * 统计黑名单数量
     */
    @Override
    public int selectAllBlackIpCount() {
        BlackIpExample blackIpExample = new BlackIpExample();
        return (int) blackIpMapper.countByExample(blackIpExample);
    }
}
